package com.wn.servlet;

import com.wn.bean.Owner;
import com.wn.service.OwnerService;
import com.wn.service.impl.OwnerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author yellow Docter
 * @date 2024 -06-18
 * @desc
 *
 *    01 servlet 开发的步骤
 *        01 创建一个类 继承HttpServlet
 *        02 重写 doGet\doPost方法
 *                 doGet 中调用doPost
 *                 统一在doPost中处理请求
 *
 *        03  写业务逻辑 拿到数据 xxx
 *        04  配置 @WebServlet("处理请求的url")
 */
@WebServlet("/owner/add")
public class OwnerServlet extends HttpServlet {

    private OwnerService ownerService = new OwnerServiceImpl();
    //doGet 处理get请求
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.doPost(req, resp);
//    }
//
//    //doPost 处理post请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /**
         * HttpServletRequest  http协议中 提交的数据都封装到了 req对象中
         *        http协议中的载荷的数据 进行了封装 
         */

        //处理中文乱码
        // 设置请求数据的编码: UTF-8
        req.setCharacterEncoding("UTF-8");

        //通过  req 获取数据
        String username = req.getParameter("username");
        String tel = req.getParameter("tel");
        String sex = req.getParameter("sex");
        String identity = req.getParameter("identity");
        String houseId = req.getParameter("houseId");
        String remarks = req.getParameter("remarks");

        // 把 所有的 数据封装一个 map的键值对中
        // Map<String, String[]> parameterMap = req.getParameterMap();
        //把键值对 通过反射 封装到 Owner Bean中



        System.out.println("remarks = " + remarks);

        Owner owner = new Owner();
        owner.setUsername(username);
        owner.setTel(tel);
        owner.setSex(Integer.parseInt(sex));
        owner.setIdentity(identity);
        owner.setHouseId(Integer.parseInt(houseId));
        owner.setRemarks(remarks);

        System.out.println("owner = " + owner);

        //jdbc 操作 可以吗?
        ownerService.saveOwner(owner);

    }
}
